﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IosDatamodel;
using System.IO;
using IOSpro.Lib;
using IOSpro.Models;
namespace IOSpro.Controllers
{
    public class VideosController : Controller
    {

        private IosDBEntities db;
        private IVideosBL iblvideos;

        public VideosController() 
        {
            db = new IosDBEntities();
            iblvideos = new VideosBL(this.ModelState);
          
        }
        public ActionResult Index()
        {
            var videos = from n in db.Videos select n;
            ViewData["shu"] =1000;
            return View(videos);
        }

        public ActionResult AddVideoInfo() 
        {
            var dplVideosheet = db.VideoSheet.ToList().Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() });
            ViewData["dplVideosheet"] = dplVideosheet;

            return View();
        
        }

        [HttpPost]
        public ActionResult AddVideoInfo(VideoInfo VideoModel)
        {

            var dplVideosheet = db.VideoSheet.ToList().Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() });
            ViewData["dplVideosheet"] = dplVideosheet;
            iblvideos.SaveVideoinfo(VideoModel, Request.Form["VideoSheets"]);
            ModelState.AddModelError("", "添加成功！");
            return View();

        }

        //
        // GET: /Videos/Details/5

        public ActionResult UploadVideo(int id)
        {
            ViewData["id"] = id;

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UploadVideo(HttpPostedFileBase fileData, int videoid)
        {
            if (fileData != null)
            {

                try
                {
                    // 文件上传后的保存路径
           //         int questionsetid1 = Convert.ToInt32(questionsetid);
                    var savepathname= (from a in db.Videos where  a.Id==videoid    join  b  in db.VideoSheet on a.VideoSheetId equals b.Id  select b.VideoUrlName).FirstOrDefault();
                    string filePath = Server.MapPath("~/Uploads/" + savepathname + "/");
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    string fileName = Path.GetFileName(fileData.FileName);// 原始文件名称               
                    var path = Path.Combine(filePath, fileName);
                    iblvideos.SaveVideourl(videoid, fileName);//添加到数据库
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(Server.MapPath(path));
                    }
                    fileData.SaveAs(path);

                    return Json(new { Success = true, FileName = fileName, SaveName = fileName });
                }
                catch (Exception ex)
                {
                    return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {

                return Json(new { Success = false, Message = "请选择要上传的文件！" });
            }
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult addvideoimage(HttpPostedFileBase fileData, int videoid)
        {
            if (fileData != null)
            {
                try
                {
                    var savepathname = (from a in db.Videos where a.Id == videoid join b in db.VideoSheet on a.VideoSheetId equals b.Id select b.VideoUrlName).FirstOrDefault();
                    // 文件上传后的保存路径
              //      int questionsetid1 = Convert.ToInt32(questionsetid);
                    string filePath = Server.MapPath("~/Uploads/" + savepathname+"/");
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    string fileName = Path.GetFileName(fileData.FileName);// 原始文件名称
                    string fileExtension = Path.GetExtension(fileName); // 文件扩展名
                    string saveName = fileName;// 保存文件名称
                    //  if()
                    var path = Path.Combine(filePath, saveName);
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(Server.MapPath(path));
                    }
                    fileData.SaveAs(path);
                    iblvideos.SaveVideoimage(videoid, fileName);
                    return Json(new { Success = true, FileName = fileName, SaveName = saveName });
                }
                catch (Exception ex)
                {
                    return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {

                return Json(new { Success = false, Message = "请选择要上传的文件！" }, JsonRequestBehavior.AllowGet);
            }
        }


       
    }
}
